package com.lp.controller.iot;

import com.lp.bean.GranpTree;
import com.lp.bean.TServerUrl;
import com.lp.bo.UserInfoBO;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.Code;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;
import com.lp.util.iot.EncryptionMD5Utils;
import com.lp.util.iot.LiveNVSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class TServerUrlController extends BaseController {

    @Autowired
    public LiveNVSUtil liveNVSUtil;

    /**
     * 检索
     * @param response
     * @param tServerUrl
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.ServerUrl.SERVER_URL_PAGE)
    public ModelAndView selectPage(HttpServletResponse response,
                                          @RequestHeader(value = ResultMapUtils.USER_KEY, required = false) String userKey ,
                                          @RequestBody TServerUrl tServerUrl,
                                          @RequestParam(required=false) Integer pageSize ,
                                          @RequestParam Integer paged ) {
        Map<String, Object> resultMap = getResultMap();
        try {
            resultMap = service.selectPageList("TServerUrl.selectPageList",getPageBean(paged,pageSize), tServerUrl);
        } catch (Exception e) {
            exception(e,resultMap, tServerUrl);
        }
        return getModelAndView(response, resultMap);
    }

    /**
     * 插入
     * @param response
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.ServerUrl.SERVER_URL)
    public ModelAndView save(HttpServletResponse response,
                             @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
                             @RequestBody TServerUrl obj ) {
        Map<String, Object> resultMap = getResultMap();
        try {
            String token = liveNVSUtil.getLoginToken(obj);
            if (token == null) {
                throw new RuntimeException("远程服务异常ip：" + obj.getIp());
            }
            obj.setCreate_time(new Date());
            obj.setPassword(EncryptionMD5Utils.encryption(obj.getPwd()));
            resultMap = service.insert("TServerUrl.insert", obj);
            if (isOk(resultMap)) {
                List<TServerUrl> urlAll = (List<TServerUrl>)getData(service.selectList("TServerUrl.selectList", new TServerUrl()));

                // 添加缓存
                ProCacheUtil.addCache(CacheName.SERVER_URL, "video_Stream_serviceAll", urlAll);

                List<GranpTree> treeArrayList = liveNVSUtil.getGranpTrees(urlAll);
                if (treeArrayList != null && treeArrayList.size() != 0) {
                    ProCacheUtil.addCache(CacheName.SERVER_URL, "get_group_tree", treeArrayList);
                }
            }
        } catch (Exception e) {
            exception(e,resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }

    /**
     * 查询单个
     * @param response
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.ServerUrl.SERVER_URL)
    public ModelAndView selectOne(HttpServletResponse response,
                                  @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
                                  @RequestParam Integer id ) {
        Map<String, Object> resultMap = getResultMap();
        try {
            TServerUrl obj = new TServerUrl();
            UserInfoBO user = getUserInfoByUserKey(userKey);
            if (ObjectUtil.isNotEmpty(user)) {
                obj.setId(id);
                resultMap = service.selectOne("TServerUrl.selectOne", obj);
            } else {
                putStatusCode(resultMap, Code.ResponseCode.SystemCode.ERROR);
            }
        } catch (Exception e) {
            exception(e,resultMap, id);
        }
        return getModelAndView(response, resultMap);
    }

    /**
     * 更新
     * @param response
     * @param obj
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.ServerUrl.SERVER_URL)
    public ModelAndView update(HttpServletResponse response,
                               @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
                               @RequestBody TServerUrl obj ) {
        Map<String, Object> resultMap = getResultMap();
        try {
            String token = liveNVSUtil.getLoginToken(obj);
            if (token == null) {
                throw new RuntimeException("远程服务异常ip：" + obj.getIp());
            }
            obj.setPassword(EncryptionMD5Utils.encryption(obj.getPwd()));
            resultMap = service.update("TServerUrl.update", obj);
            if (isOk(resultMap)) {
                // 添加缓存
                ProCacheUtil.addCache(CacheName.SERVER_URL, obj.getId().toString(), obj);
            }

            List<TServerUrl> urlAll = (List<TServerUrl>)getData(service.selectList("TServerUrl.selectList", new TServerUrl()));

            // 添加缓存
            ProCacheUtil.addCache(CacheName.SERVER_URL, "video_Stream_serviceAll", urlAll);

            List<GranpTree> treeArrayList = liveNVSUtil.getGranpTrees(urlAll);
            if (treeArrayList != null && treeArrayList.size() != 0) {
                ProCacheUtil.addCache(CacheName.SERVER_URL, "get_group_tree", treeArrayList);
            }
        } catch (Exception e) {
            exception(e,resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }

    /**
     * 删除
     * @param response
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.ServerUrl.SERVER_URL)
    public ModelAndView delete(HttpServletResponse response,
                               @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
                               @RequestParam Integer id ) {
        Map<String, Object> resultMap = getResultMap();
        try {
            TServerUrl obj = new TServerUrl();
            obj.setId(id);
            resultMap = service.delete("TServerUrl.delete", obj);
            if (isOk(resultMap)) {
                // 添加缓存
                ProCacheUtil.removeCache(CacheName.SERVER_URL, obj.getId().toString());
            }
        } catch (Exception e) {
            exception(e,resultMap, id);
        }
        return getModelAndView(response, resultMap);
    }
}
